package com.lmpop.frame.util;

import java.io.PrintStream;

public class PageTools
{
  public String getPageTools(String record_Sum, String page_Size, String page, int current)
  {
    int pagination = 0;
    int page_Down = 1;
    String first = ""; String end = ""; String up = ""; String down = "";
    if (Integer.parseInt(record_Sum) % Integer.parseInt(page_Size) == 0) {
      pagination = Integer.parseInt(record_Sum) / Integer.parseInt(page_Size);
    }
    else {
      pagination = Integer.parseInt(record_Sum) / Integer.parseInt(page_Size) + 1;
    }
    if (current > pagination) {
      current = pagination;
    }
    if (Integer.parseInt(record_Sum) == 0) {
      first = "首页";
      up = "上一页";
      down = "下一页";
      end = "尾页";
    }
    else if (current == 1) {
      if (pagination > 1) {
        page_Down = current + 1;
        first = "首页";
        up = "上一页";
        down = new StringBuilder().append("<a href='").append(page).append(page_Down).append("' style=color:#3300FF>下一页</a>").toString();
        end = new StringBuilder().append("<a href='").append(page).append(pagination).append("' style=color:#3300FF>尾页</a>").toString();
      }
      else {
        first = "首页";
        up = "上一页";
        down = "下一页";
        end = "尾页";
      }
    }
    else if (current == pagination) {
      first = new StringBuilder().append("<a href='").append(page).append("1' style=color:#3300FF>").append("首页</a>").toString();
      up = new StringBuilder().append("<a href='").append(page).append(current - 1).append("' style=color:#3300FF>上一页</a>").toString();
      down = "下一页";
      end = "尾页";
    }
    else {
      first = new StringBuilder().append("<a href='").append(page).append("1' style=color:#3300FF>").append("首页</a>").toString();
      up = new StringBuilder().append("<a href='").append(page).append(current - 1).append("' style=color:#3300FF>上一页</a>").toString();
      down = new StringBuilder().append("<a href='").append(page).append(current + 1).append("' style=color:#3300FF>下一页</a>").toString();
      end = new StringBuilder().append("<a href='").append(page).append(pagination).append("' style=color:#3300FF>尾页</a>").toString();
    }
    String pageTools = new StringBuilder().append("<td align='left' colspan='2' style='font-weight:bold; padding:2px 5px;'>共<span style=color:#3300FF>").append(record_Sum).append("</span>条 &nbsp;共<span style=color:#3300FF>").append(pagination).append("</span>页").append("&nbsp;第<span style=color:#FF0000>").append(current).append("</span>页").append("</td>").append("<td align='right' colspan='5' style='padding:2px 5px;font-weight: bold;'>").append(first).append("&nbsp; &nbsp;").append(up).append(" &nbsp;").append(down).append("&nbsp; ").append(end).append("</td>").toString();
    return pageTools;
  }

  public static void main(String[] args)
  {
    System.out.println(new PageTools().getGoogleToolsBar(137, "page", 4, 5));
  }

  public String getGoogleToolsBar(int recordSum, String linkPage, int current, int size)
  {
    String toolsBar = "";
    int next = 0;
    next = current + 1;
    int pre = 0;
    pre = current - 1;

    int pageSize = 0;
    if (recordSum % size == 0) {
      pageSize = recordSum / size;
    }
    else {
      pageSize = recordSum / size + 1;
    }

    if (current > pageSize) {
      current = pageSize;
    }
    if (current == 1)
    {
      if (pageSize > 1) {
        for (int i = 1; (i < 10) && (i <= pageSize); i++) {
          if (i == current) {
            page p = new page();
            p.setNum(new StringBuilder().append(i).append("").toString());
            p.setLink(new StringBuilder().append("<a href=").append(linkPage).append(i).append(" style='font-weight:bold;color:#FF3300'>").toString());
            toolsBar = new StringBuilder().append("首页 上一页&nbsp;&nbsp;").append(toolsBar).append(p.toString()).append("&nbsp;&nbsp;").toString();
          }
          else {
            page p = new page();
            p.setNum(new StringBuilder().append(i).append("").toString());
            p.setLink(new StringBuilder().append("<a href=").append(linkPage).append(i).append(">").toString());
            toolsBar = new StringBuilder().append(toolsBar).append(p.toString()).append("&nbsp;&nbsp;").toString();
          }
        }

        toolsBar = new StringBuilder().append(toolsBar).append("&nbsp;&nbsp;<a href=\"").append(linkPage).append(next).append("\">下一页</a>&nbsp;&nbsp;<a href=\"").append(linkPage).append(pageSize).append("\">末页</a>").toString();
      }
    }
    else if (current == pageSize)
    {
      for (int i = pageSize; (i > pageSize - 10) && 
        (i != 0); i--)
      {
        if (i == current) {
          page p = new page();
          p.setNum(new StringBuilder().append(i).append("").toString());
          p.setLink(new StringBuilder().append("<a href=").append(linkPage).append(i).append(" style='font-weight:bold;color:#FF3300'>").toString());
          toolsBar = new StringBuilder().append(p.toString()).append("&nbsp;").append(toolsBar).append("&nbsp;末页 下一页").toString();
        }
        else {
          page p = new page();
          p.setNum(new StringBuilder().append(i).append("").toString());
          p.setLink(new StringBuilder().append("<a href=").append(linkPage).append(i).append(">").toString());
          toolsBar = new StringBuilder().append(p.toString()).append("&nbsp;&nbsp;").append(toolsBar).toString();
        }
      }

      toolsBar = new StringBuilder().append("<a href=\"").append(linkPage).append("1\">首页</a>&nbsp;&nbsp;<a href=\"").append(linkPage).append("").append(pre).append("\">上一页</a>&nbsp;&nbsp;").append(toolsBar).toString();
    }
    else {
      return pagination(recordSum, size, current, linkPage);
    }
    toolsBar = new StringBuilder().append("共").append(recordSum).append("条 &nbsp;").append(pageSize).append("页&nbsp;&nbsp;").append(toolsBar).toString();
    return toolsBar;
  }

  public String pagination(int total, int per, int page, String query_string)
  {
    int allpage = 0;
    int next = 0;
    int pre = 0;
    int startcount = 0;
    int endcount = 0;
    String pagestr = "";

    if (page < 1) {
      page = 1;
    }

    if (per != 0) {
      allpage = total / per;
      allpage = total % per != 0 ? allpage + 1 : allpage;
      allpage = allpage == 0 ? 1 : allpage;
    }
    next = page + 1;
    pre = page - 1;
    startcount = page + 5 > allpage ? allpage - 9 : page - 4;

    endcount = page < 5 ? 10 : page + 5;
    if (startcount < 1) {
      startcount = 1;
    }
    if (allpage < endcount) {
      endcount = allpage;
    }
    pagestr = new StringBuilder().append("共").append(total).append("条&nbsp;&nbsp;共").append(allpage).append("页&nbsp;&nbsp;&nbsp;").toString();

    pagestr = new StringBuilder().append(pagestr).append(page > 1 ? new StringBuilder().append("<a href=\"").append(query_string).append("1\">首页</a>&nbsp;&nbsp;<a href=\"").append(query_string).append("").append(pre).append("\">上一页</a>").toString() : "首页 上一页").toString();

    for (int i = startcount; i <= endcount; i++) {
      pagestr = new StringBuilder().append(pagestr).append(page == i ? new StringBuilder().append("&nbsp;&nbsp;<font color=\"#ff0000\">[").append(i).append("]</font>").toString() : new StringBuilder().append("&nbsp;&nbsp;<a href=\"").append(query_string).append(i).append("\">[").append(i).append("]</a>").toString()).toString();
    }
    pagestr = new StringBuilder().append(pagestr).append(page != allpage ? new StringBuilder().append("&nbsp;&nbsp;<a href=\"").append(query_string).append(next).append("\">下一页</a>&nbsp;&nbsp;<a href=\"").append(query_string).append(allpage).append("\">末页</a>").toString() : " 下一页 末页").toString();

    return pagestr;
  }

  public String getNongcunToolsBar(int recordSum, String linkPage, int current, int size)
  {
    String toolsBar = "";
    int next = 0;
    next = current + 1;
    int pre = 0;
    pre = current - 1;

    int pageSize = 0;
    if (recordSum % size == 0) {
      pageSize = recordSum / size;
    }
    else {
      pageSize = recordSum / size + 1;
    }

    if (current > pageSize) {
      current = pageSize;
    }

    return nongcun_pagination(recordSum, size, current, linkPage);
  }

  public String nongcun_pagination(int total, int per, int page, String query_string)
  {
    int allpage = 0;
    int next = 0;
    int pre = 0;
    int startcount = 0;
    int endcount = 0;
    String pagestr = "";

    if (page < 1) {
      page = 1;
    }

    if (per != 0) {
      allpage = total / per;
      allpage = total % per != 0 ? allpage + 1 : allpage;
      allpage = allpage == 0 ? 1 : allpage;
    }
    next = page + 1;
    pre = page - 1;
    startcount = page + 5 > allpage ? allpage - 9 : page - 4;

    endcount = page < 5 ? 10 : page + 5;
    if (startcount < 1) {
      startcount = 1;
    }
    if (allpage < endcount) {
      endcount = allpage;
    }

    pagestr = new StringBuilder().append(pagestr).append(page > 1 ? new StringBuilder().append("<a href=\"").append(query_string).append("1\">首页</a><a href=\"").append(query_string).append("").append(pre).append("\">上一页</a>").toString() : new StringBuilder().append("<a href=\"").append(query_string).append("1\">首页</a>").toString()).toString();

    for (int i = startcount; i <= endcount; i++) {
      pagestr = new StringBuilder().append(pagestr).append(page == i ? new StringBuilder().append("<a href='#' class='page_current'>").append(i).append("</a>").toString() : new StringBuilder().append("<a href=\"").append(query_string).append(i).append("\">").append(i).append("</a>").toString()).toString();
    }
    pagestr = new StringBuilder().append(pagestr).append(page != allpage ? new StringBuilder().append("<a href=\"").append(query_string).append(next).append("\">下一页</a><a href=\"").append(query_string).append(allpage).append("\">末页</a>").toString() : new StringBuilder().append("<a href=\"").append(query_string).append(allpage).append("\">末页</a>").toString()).toString();

    return pagestr;
  }

  class nongcun_page extends PageTools.page
  {
    private String head;
    private String bottom;

    public nongcun_page()
    {
      super();
      this.head = "";
      this.bottom = "";
    }
  }

  class page
  {
    private String head;
    private String bottom;
    private String num;
    private String link;

    public page()
    {
      this.head = "[";
      this.bottom = "]";
    }

    public String getNum()
    {
      return this.num;
    }

    public void setNum(String num)
    {
      this.num = num;
    }

    public String toString()
    {
      return this.link + this.head + this.num + this.bottom + "</a>";
    }

    public String getHead()
    {
      return this.head;
    }

    public void setHead(String head)
    {
      this.head = head;
    }

    public String getBottom()
    {
      return this.bottom;
    }

    public void setBottom(String bottom)
    {
      this.bottom = bottom;
    }

    public String getLink()
    {
      return this.link;
    }

    public void setLink(String link)
    {
      this.link = link;
    }
  }
}